<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Mod_master_user extends CI_Model{ 

	var $tabel_name = 'master_user';

    function __construct() {
        parent::__construct();
    }

	public function getall(){
		$ambil_data = $this->db->get('master_user');//mengambil tabel master_user
		
		if ($ambil_data->num_rows() > 0 ){ //jika data lebih dari 0
			foreach ($ambil_data->result() as $data){
				$hasil[] = $data;
			}
			return $hasil;
		}
	}
    
    	function getdata_json(){//mengambil data berdasar seluruh isi tabel
        $this->db->select('*')->from('master_user')->order_by('ID_USER');
        $query=$this->db->get();
        return $query->result_array();
    }

	public function tambah(){
		$id_user = $this->input->post('id_user');		
		$nama  = $this->input->post('nama');	
		$password = $this->input->post('password');
		$tanggal_lahir = $this->input->post('tanggal_lahir');
		$tempat_lahir = $this->input->post('tempat_lahir');
		$role = $this->input->post('role');
                
                
		$data = array (
			'id_user'=> $id_user,		
			'nama'=>$nama,	
			'password'=>md5($password),
			'tanggal_lahir'=>date('Y-m-d',strtotime($tanggal_lahir)),
			'tempat_lahir'=>$tempat_lahir,
			'role'=>$role,
            );	
            
		$this->db->where('id_user',$id_user);
		$this->db->insert('master_user', $data);
        $id = $this->db->insert_id();
        $this->db->where('id_user',$id);
        if ($role=="admin"){$role=1;} else {$role=2;}
        $this->db->update('master_user',array('kode_pegawai'=>$id.$role.date('dm',strtotime($tanggal_lahir)) ));              
	}

	public function update($id_user){	
		$id_user=$this->input->post('id_user');		 
		$nama=$this->input->post('nama');	 
		$password=$this->input->post('password');
		$tanggal_lahir=$this->input->post('tanggal_lahir');
		$tempat_lahir=$this->input->post('tempat_lahir');
		$role=$this->input->post('role');
		  $data = array (
			'id_user' => $id_user,		
			'nama'  => $nama,	
			'password'=> $password,
			'tanggal_lahir'=> $tanggal_lahir,
			'tempat_lahir'=> $tempat_lahir,
			'role'=>$role
		);
		$this->db->where('id_user',$id_user);
		$this->db->update('master_user',$data);	//update data 
	}

	public function getById($id_user){	//mengambil data dari db berdasarkan   id (primary key)
		return $this->db->get_where('master_user',array('id_user'=>$id_user))->row();
	}

	public function delete($id_user){	
		$this->db->where('id_user',$id_user);
		$this->db->delete('master_user'); //query delete data 
	}

	public function cek_user_login($username, $password) {
        $this->db->select('*');
        $this->db->where('NAMA', $username);
        $this->db->where('PASSWORD', md5($password));

        $query = $this->db->get($this->tabel_name, 1);



        if ($query->num_rows() == 1) {
            $this->db->limit(1);
            return $query->row_array();
        }
    }
    
    public function cek_user_login_barcode($barcode) {
        $this->db->select('*');
        $this->db->where('KODE_PEGAWAI', $barcode);
       

        $query = $this->db->get($this->tabel_name, 1);



        if ($query->num_rows() == 1) {
            $this->db->limit(1);
            return $query->row_array();
        }
    }

    public function validasi()
    {
        $nama = $this->input->post('nama');
        $password = $this->input->post('password');
        $check = $this->mod_master_user->check($nama, md5($password));
        if($check->num_rows() > 0)
        {
            //login berhasil, buat session
            //$this->session->set_userdata('username',$username);
            redirect('master_user');
        }
        else
        {
            //login gagal
            //$this->session->set_flashdata('message','Username atau password salah');
            redirect('users');
        }
    }

}
